package com.qst.express.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qst.commonUtils.result.R;
import com.qst.express.entity.Courier;
import com.qst.express.entity.Express;
import com.qst.express.entity.vo.CourierVo;
import com.qst.express.entity.vo.ExpressVo;
import com.qst.express.service.CourierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mjh
 * @since 2022-09-25
 */
@Api(description = "快递员管理")
@RestController
@RequestMapping("/e/courier")
@CrossOrigin
public class CourierController {

    @Autowired
    private CourierService courierService;

    /**
     * 用于查询数据库中的全部快递员（总数+日注册量）
     *
     * @return [{size:总数,day:新增}]
     */
    @GetMapping("/console")
    public R console() {
        List<Map<String, String>> data = courierService.console();
        return R.ok().data("courierTotal", data);
    }

    /**
     * TODO 条件查询带分页
     *
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "快递员分页查询")
    @PostMapping("/pageTeacherCondition/{page}/{limit}")
    public R pageTeacherCondition(@PathVariable("page") Integer page,
                                  @PathVariable("limit") Integer limit,
                                  @RequestBody(required = false) CourierVo courierVo) {

        Page<Courier> courierPage = new Page<>(page, limit);
        IPage<Courier> teacherIPage = courierService.pageTeacherCondition(courierPage, courierVo);
        System.out.println(courierPage);
        return R.ok().data("total", teacherIPage.getTotal()).data("rows", teacherIPage.getRecords());
    }

    /**
     * 添加快递员
     * @param courier
     * @return
     */
    @ApiOperation(value = "添加快递员")
    @PostMapping("/addCourier")
    public R addCourier(@RequestBody(required = false) Courier courier) {
        courier.setCreatetime(new Date());
        boolean save = courierService.save(courier);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 删除快递员
     * @param courierId
     * @return
     */
    @ApiOperation(value = "删除快递员")
    @DeleteMapping("/removeCourier/{id}")
    public R removeCourierId(@PathVariable(value = "id") String courierId) {
        boolean remove = courierService.removeById(Integer.parseInt(courierId));
        if (remove) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    /**
     * 批量删除快递员
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除快递员")
    @PostMapping("/deletes")
    public R deltes(@RequestBody(required = false) Integer[] ids) {
        System.out.println(ids);
        boolean remove = false;
        for (Integer id : ids) {
             remove = courierService.removeById(id);
        }
        if (remove) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 通过id查询快递员信息
     * @param courierId
     * @return
     */
    @ApiOperation(value = "通过id查询快递员信息")
    @GetMapping("/findCourierById/{id}")
    public R findCourierById(@PathVariable(value = "id") String courierId) {
        Courier courier = courierService.getById(courierId);
        if (courier != null) {
            return R.ok().data("courier", courier);
        } else {
            return R.error();
        }
    }


    /**
     * 更新快递员信息
     * @param courier
     * @return
     */
    @ApiOperation(value = "更新快递员信息")
    @PostMapping("/updateCourierById")
    public R updateCourierById(@RequestBody(required = false) Courier courier){
        boolean update = courierService.updateById(courier);
        if (update) {
            return R.ok();
        } else {
            return R.error();
        }
    }

}

